package net.zoneland.geological.system.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zoneland.geological.station.dao.Tbl_station_infoDao;
import net.zoneland.geological.system.dao.Tbl_areaDao;
import net.zoneland.geological.system.entity.Tbl_area;
import net.zoneland.geological.system.entity.ZTreeEntity;

/**
 * tbl_area[区域信息表]表操作Serfice实现类

 * 表   名：TBL_AREASERVICEIMPL<br/>
 * 注   释：区域信息表<br/>
 * 作   者：李义<br/>
 * 日   期：2017-05-28 23:06:48
**/
@Service("tbl_areaService")
public class Tbl_areaServiceImpl implements Tbl_areaServiceI { 

	public static Logger logger = Logger.getLogger(Tbl_areaServiceImpl.class);

	/**
	 * 自动注入DAO对象
	**/
	@Autowired
	private Tbl_areaDao tbl_areaDao;
	
	@Autowired
	private Tbl_station_infoDao tbl_station_infoDao;

	/**
	 * 新增或者更新数据方法接口
	**/
	@Override
	public Tbl_area addOrUpdate( Tbl_area tbl_area )  throws Exception{
		if ( tbl_area == null ) {
			logger.error(Tbl_area.class.getName() + "对象参数信息为NULL，addOrUpdate方法无法继续！");
			return tbl_area;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if( tbl_area.getId() ==null || "".equals( tbl_area.getId().trim() ) ){
			map.put( "id", "0000-0000-0000-0000" );
		}else{
			map.put( "id", tbl_area.getId() );
		}
		List<Tbl_area> list  = tbl_areaDao.getList(map);
		if( list != null && !list.isEmpty() ){
			tbl_areaDao.edit( tbl_area );
			return tbl_area;
		}else{
			if( tbl_area.getId() ==null || tbl_area.getId().trim().isEmpty() ){
				tbl_area.setId( UUID.randomUUID() + "" );
		    }
			tbl_areaDao.add( tbl_area );
			return tbl_area;
		}
	}
	/**
	 * 新增数据方法实现
	**/
	@Override
	public boolean add(Tbl_area tbl_area) throws Exception{
		tbl_areaDao.add( tbl_area );
		return true;
	}
	/**
	 * 更新数据方法实现
	**/
	@Override
	public boolean update(Tbl_area tbl_area) throws Exception{
		tbl_areaDao.edit( tbl_area );
		return true;
	}
	/**
	 * 查询数据方法实现
	**/
	@Override
	public Tbl_area get( Map<String, Object> map ) throws Exception{
		return tbl_areaDao.get( map );
	}
	/**
	 * 查询数据方法实现
	**/
	@Override
	public Tbl_area getWithCode( Map<String, Object> map ) throws Exception{
		return tbl_areaDao.getWithCode( map );
	}
	/**
	 * 查询数据方法实现（列表）
	**/
	@Override
	public List<Tbl_area> getList(Map<String, Object> map) throws Exception{ 
		return tbl_areaDao.getList( map );
	}
	
	/**
	 * 查询数据总数方法实现
	**/
	@Override
	public int getListCount(Map<String, Object> map) throws Exception{ 
		return tbl_areaDao.getListCount( map );
	}
	
	/**
	 * 数据删除方法实现
	**/
	@Override
	public boolean delete(Map<String, Object> map) throws Exception{ 
		tbl_areaDao.delete( map );
		return true;
	}
	
	@Override
	public List<String> getAreaCodesSupNested( String area_code ) {
		List<String> area_codes = new ArrayList<>();
		return getAreaCodesSupNested( area_code, area_codes );
	}
	
	private List<String> getAreaCodesSupNested(String area_code, List<String> area_codes) {
		if( area_codes == null ) {
			area_codes = new ArrayList<>();
		}
		Map<String, Object> map = new HashMap<>();
		map.put( "area_code", area_code );
		Tbl_area area = tbl_areaDao.get( map );
		if( area != null ) {
			if( area_codes.contains( area.getArea_code() )) {
				area_codes.add( area.getArea_code() );
			}
			return getAreaCodesSupNested( area.getParent_code(), area_codes );
		}
		return area_codes;
	}
}
